﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Composition;
using BTGICS.CommonLibrary;
using System.IO;

namespace BTGICS.AceyDuceyXL
{
    [Export( typeof( IPlugIn ) )]
    public class PlugIn : IPlugIn
    {
        // original by Bill Palmby for Creative Computing
        // this version has ability to save/load games, quit, and tracks winning/losing stats.
        #region Private Properties
        private int Bank = 0;
        private bool GameIsOver = false;
        private Random rnd = new Random();
        private int[] Cards = new int[] { 0, 0 };
        private int Bet = 0;
        private int Wins = 0;
        private int Losses = 0;
        private int Turns = 0;
        private int PercentWin
        {
            get
            {
                try
                {
                    if( Turns == 0 )
                    {
                        return 0;
                    }
                    return ( int ) ( 100f * ( ( float ) Wins / ( float ) Turns ) );
                }
                catch
                {
                    return 0;
                }
            }
        }
        private int PercentLoss
        {
            get
            {
                try
                {
                    if( Turns == 0 )
                    {
                        return 0;
                    }
                    return ( int ) ( 100f * ( ( float ) Losses / ( float ) Turns ) );
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion
        #region IPlugIn Members
        public string Name
        {
            get
            {
                return "Acey Ducey XL";
            }
        }
        public string Description
        {
            get
            {
                return "Original Acey Ducey by Bill Palmby for Ceative Computing.\r\n\tThis version has ability to save/load games, quit, and tracks winning\r\n\tand losing stats.";
            }
        }
        public void Run()
        {
            Console.ResetColor();
            Console.Clear();
            Introduction();
            Console.ForegroundColor = ConsoleColor.Green;
            string ans = Console.ReadLine();
            Console.ResetColor();
            if( ans.ToUpper() == "Y" )
            {
                Start();
                while( true )
                {
                    if( GameIsOver )
                    {
                        break;
                    }
                    TitleBar();
                    TwoNextCards();
                    WhatIsYourBet();
                    DealThirdCard();
                    Turns++;
                    Console.WriteLine( "Press ENTER to deal next cards, or S to save, L to load, Q to quit." );
                    Console.ForegroundColor = ConsoleColor.Green;
                    ans = Console.ReadLine();
                    Console.ResetColor();
                    switch( ans.ToUpper() )
                    {
                        case "S":
                            SaveGame();
                            Console.WriteLine( "Press ENTER to deal next cards." );
                            Console.ReadLine();
                            break;
                        case "L":
                            LoadGame();
                            Console.WriteLine( "Press ENTER to deal next cards." );
                            Console.ReadLine();
                            break;
                        case "Q":
                            GameIsOver = true;                            
                            break;
                    }
                    CheckForGameOver();

                }
            }
            Console.WriteLine( "Good Bye" );
        }
        #endregion
        #region Save Game
        private void SaveGame()
        {
            string fileSpec = Path.Combine(Directory.GetCurrentDirectory(), "AceyDuceyXL.dat");
            if( File.Exists( fileSpec ) )
            {
                File.Delete( fileSpec );
            }
            TextWriter wr = File.CreateText(fileSpec );
            wr.WriteLine( Bank );
            wr.WriteLine( Wins );
            wr.WriteLine( Losses );
            wr.WriteLine( Turns );
            wr.Flush();
            wr.Close();
            Console.WriteLine( "Game saved." );
       
        }
        #endregion
        #region Load Game
        private void LoadGame()
        {
            string fileSpec = Path.Combine( Directory.GetCurrentDirectory(), "AceyDuceyXL.dat" );
            if( File.Exists( fileSpec ) )
            {

                TextReader rd = File.OpenText( fileSpec );
                int b = 0;
                try
                {
                    b = int.Parse( rd.ReadLine() );
                    Bank = b;
                    b = int.Parse( rd.ReadLine() );
                    Wins = b;
                    b = int.Parse( rd.ReadLine() );
                    Losses = b;
                    b = int.Parse( rd.ReadLine() );
                    Turns = b;
                    Console.WriteLine( "Game Loaded." );
                }
                catch
                {
                }
                rd.Close();
            }
                                
        }
        #endregion
        #region Introduction
        private void Introduction()
        {
            Console.WriteLine( "Acey Ducey XL is played int the following manner:" );
            Console.WriteLine( "The dealer (computer) deals two cards face up and you have an option to bet or\r\nnot bet depending on whether or not you feel the card will have a value between\r\nthe first two." );
            Console.WriteLine( "If you do not want to bet, input a 0 (zero)." );
            Console.WriteLine( "Do you wish to play? (Enter Y or N)" );
        }
        #endregion
        #region Start
        private void Start()
        {
                Bank = 100;
                GameIsOver = false;
                Wins = 0;
                Losses = 0;
                Turns = 0;
        }
        #endregion
        #region TitleBar
        private void TitleBar()
        {
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine( "ACEY DUCEY XL [${0}.00] [Wins:{1},{2}%] [Losses:{3},{2}%] [Turns:{4}]",
                Bank, Wins, PercentWin, Losses, PercentLoss, Turns );
            Console.ResetColor();
        }
        #endregion
        #region Two Next Cards
        private void TwoNextCards()
        {
            Console.WriteLine( "Here are your cards:" );
            Cards[ 0 ] = rnd.Next( 13 ) + 2;
            Cards[ 1 ] = Cards[ 0 ];
            while( Cards[ 1 ] <= Cards[ 0 ] )
            {
                Cards[ 1 ] = rnd.Next( 13 ) + 2;
            }
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine( "{0}\r\n{1}", SelectCard( Cards[ 0 ] ), SelectCard( Cards[ 1 ] ) );
            Console.ResetColor();
        }
        #endregion
        #region Select Card
        private string SelectCard( int cardNumber )
        {
            if( cardNumber < 11 )
            {
                return  cardNumber.ToString();
            }
            switch( cardNumber )
            {
                case 11:
                    return "Jack";
                case 12:
                    return "Queen";
                case 13:
                    return "King";
            }
            return "Ace";
        }
        #endregion
        #region What Is Your Bet
        private void WhatIsYourBet()
        {
            Console.Write( "What is your bet? " );
            GetBet();
        }
        #endregion
        #region Deal Third Card
        private void DealThirdCard()
        {
            int ThirdCard = rnd.Next( 13 ) + 2;
            if(ThirdCard > Cards[ 0 ] && ThirdCard < Cards[ 1 ] )
            {
                Bank += ( Bet * 100 );
                Wins++;
                TitleBar();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.BackgroundColor = ConsoleColor.White;
                Console.WriteLine( "{0}", SelectCard( ThirdCard ) );
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine( "Winner!!!" );
                Console.ResetColor();
            }
            else
            {
                Losses++;
                TitleBar();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.BackgroundColor = ConsoleColor.White;
                Console.WriteLine( "{0}", SelectCard( ThirdCard ) );
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine( "Sorry, you lose." );
                Console.ResetColor();
            }
        }
        #endregion
        #region Check For Game Over
        private void CheckForGameOver()
        {
            if( Bank == 0 )
            {
                Console.WriteLine( "Sorry friend, but you blew your wad." );
                Console.Write( "Would you like to try again? (Y or N)" );
                Console.ForegroundColor = ConsoleColor.Green;
                string ans = Console.ReadLine();
                Console.ResetColor();
                if( ans.ToUpper() == "Y" )
                {
                    Start();
                }
                else
                {
                    GameIsOver = true;
                }
            }
        }
        #endregion
        #region Get Bet
        private void GetBet()
        {
            while( true )
            {
                Console.ForegroundColor = ConsoleColor.Green;
                string ans = Console.ReadLine();
                Console.ResetColor();
                try
                {
                    Bet =  int.Parse( ans );
                    if( Bet > Bank )
                    {
                        Console.WriteLine( "Sorry friend, but you bet too much" );
                        Console.WriteLine( "You only have ${0}.00.", Bank );
                    }
                    else
                    {
                       
                        Bank -= Bet;
                        break;
                    }
                }
                catch
                {
                    switch( rnd.Next( 5 ) )
                    {
                        case 0:
                            Console.WriteLine( "Last time I checked, {0} was not a number.", ans );
                            break;
                        case 1:
                            Console.WriteLine( "Please enter a number between 0 and {0}.", Bank);
                            break;
                        case 2:
                            Console.WriteLine( "Huh?" );
                            break;
                        case 3:
                            Console.WriteLine( "Sorry, please enter a number." );
                            break;
                        case 4:
                            Console.WriteLine( "Only numbers please." );
                            break;
                    }
                }
            }

        }
        #endregion
    }
}
